package com.permissionmanager.service.impl;

import com.permissionmanager.entity.Page;
import com.permissionmanager.entity.Perm;
import com.permissionmanager.entity.PermRole;
import com.permissionmanager.entity.Role;
import com.permissionmanager.exception.ServiceException;
import com.permissionmanager.mapper.PermMapper;
import com.permissionmanager.mapper.PermRoleMapper;
import com.permissionmanager.mapper.RoleMapper;
import com.permissionmanager.service.RoleService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * role业务层
 *
 */
@Service
@RequiredArgsConstructor
public class RoleServiceImpl implements RoleService {
    private final RoleMapper roleMapper;
    private final PermRoleMapper permRoleMapper;
    private final PermMapper permMapper;

    /**
     * 查询
     */
    @Override
    public List<Role> findRole(Integer id, String roleName) {
        return roleMapper.findRole(id,roleName);
    }

    /**
     * 新增
     */
    @Override
    public void add(Role role) {
        // 判断分配角色编号是否已存在
        Role role1 = roleMapper.findPermRoleId(role.getPermRoleId());
        if (role1 != null) {
            throw new ServiceException("该角色编号已存在，请重新输入！");
        }
        roleMapper.add(role);
    }

    /**
     * 修改
     * @param role （id）
     */
    @Override
    public void update(Role role) {
        // 判断分配角色编号是否已存在
        Role role1 = roleMapper.findPermRoleId(role.getPermRoleId());
        if (role1 != null) {
            throw new ServiceException("该角色编号已存在，请重新输入！");
        }
        roleMapper.update(role);
    }

    @Override
    public void del(Integer id) {
        roleMapper.del(id);
    }

    @Override
    public Page<Role> findPage(Integer pageNum, Integer pageSize, String roleName) {
        Integer skipNum = (pageNum - 1) * pageSize;
        Page<Role> page = new Page<>();
        List<Role> list = roleMapper.findPage(skipNum, pageSize, roleName);

        for (Role role : list) {
            // 查出权限
            List<PermRole> permRoleList = permRoleMapper.findPermRole(null, role.getPermRoleId());
            if (permRoleList == null) {
                return null;
            }
            List<Integer> permList = new ArrayList<>();
            for (PermRole permRole : permRoleList) {
                List<Perm> perm = permMapper.findPerm(permRole.getPermId(), null, null, null, 1);
                permList.add(perm.get(0).getId());
            }
            role.setPerm(permList);
        }
        page.setTList(list);
        page.setTotal(list.size());
        return page;
    }

    @Override
    public void allot(Integer permRoleId, List<Integer> permIdList) {
        if (permRoleId == 1 || permRoleId == 0) {
            throw new ServiceException("该用户为管理员和普通用户，不能进行修改！");
        }
        // 通过permRoleId查出数据通过id删除
        List<PermRole> permRoleList = permRoleMapper.findPermRole(null, permRoleId);
        if (permRoleList != null) {
            for (PermRole permRole : permRoleList) {
                permRoleMapper.del(permRole.getId());
            }
        }
        // 分配权限
        for (Integer i : permIdList) {
            PermRole permRole = new PermRole();
            permRole.setRoleId(permRoleId);
            permRole.setPermId(i);
            permRoleMapper.add(permRole);
        }
    }
}
